package m202401.d16;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/1/16 16:43
 */
//199. 二叉树的右视图
public class Q199 {

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> rs = new LinkedList<>();
        if (root == null) {
            return rs;
        }
        LinkedList<TreeNode> list = new LinkedList<>();
        list.addLast(root);
        list.addLast(null);
        boolean flag = true;
        while (!list.isEmpty()) {
            TreeNode node = list.removeFirst();
            if (node == null) {
                if (list.isEmpty()) {
                    break;
                }
                list.addLast(null);
                flag = true;
                continue;
            }
            if (flag) {
                rs.add(node.val);
                flag = false;
            }
            if (node.right != null) {
                list.addLast(node.right);
            }
            if (node.left != null) {
                list.addLast(node.left);
            }
        }
        return rs;
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
